/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kifas.service;

import com.kifas.model.xml.BukuPiutangModel;
import com.kifas.model.xml.CustomerModel;
import com.kifas.model.xml.PenjualanModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.PiutangModel;
import com.kifas.ui.util.ServiceConstants;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;
import java.util.List;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;

/**
 *
 * @author dhamarsu
 */
public class PenjualanService {
    private static Logger log = Logger.getLogger(MasterService.class);
    private String serviceUrl  = ServiceConstants.URL_CONTROLLER;
    private Client restClient;

    public PenjualanService() {
        restClient = Client.create();
    }

    public PersistStatus setPenjualan(PenjualanModel pm){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PENJUALAN);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, pm);
        return  persistStatus;
    }

    public List<PenjualanModel> getPenjualans() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PENJUALAN);
        return webResource.get(new GenericType<List<PenjualanModel>>(){});
    }

    public List<PenjualanModel> getPenjualansByCustomers(Long idCust) {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PENJUALAN_BY_CUSTOMER + "/" + idCust);
        return webResource.get(new GenericType<List<PenjualanModel>>(){});
    }

    public List<PenjualanModel> getPenjualansIsClosed(boolean isClosed) {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PENJUALAN_IS_CLOSED + "/" + isClosed);
        return webResource.get(new GenericType<List<PenjualanModel>>(){});
    }

    public List<PenjualanModel> getPenjualansUnsent() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PENJUALAN_UNSENT);
        return webResource.get(new GenericType<List<PenjualanModel>>(){});
    }

    public List<PenjualanModel> getPenjualansByDate(String startDate, String endDate) {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PENJUALAN_DATE);
        Form params = new Form();
        params.add("startDate", startDate);
        params.add("endDate", endDate);
        return webResource.queryParams(params).get(new GenericType<List<PenjualanModel>>(){});
    }

    public List<PenjualanModel> getPenjualansByDateStatusSent(String startDate, String endDate, boolean sentStatus) {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PENJUALAN_DATE_STATUS_SENT);
        Form params = new Form();
        params.add("startDate", startDate);
        params.add("endDate", endDate);
        params.add("sentStatus", sentStatus);
        return webResource.queryParams(params).get(new GenericType<List<PenjualanModel>>(){});
    }

    public List<PenjualanModel> getPenjualansJthTempo(String startDate) {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PENJUALAN_DATE_JTH_TEMPO);
        Form params = new Form();
        params.add("startDate", startDate);
        return webResource.queryParams(params).get(new GenericType<List<PenjualanModel>>(){});
    }

    public PersistStatus setPiutang(PiutangModel pm){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PIUTANG);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, pm);
        return  persistStatus;
    }

    public List<PiutangModel> getPiutangs() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PIUTANG);
        return webResource.get(new GenericType<List<PiutangModel>>(){});
    }

    public PersistStatus setBukuPiutang(BukuPiutangModel pm){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_BUKU_PIUTANG);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, pm);
        return  persistStatus;
    }

    public List<PiutangModel> getPiutangsJthTempoBlmLunas(){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PIUTANG_JTHTEMPO_BLMLNS);
        return webResource.get(new GenericType<List<PiutangModel>>(){});
    }

    public List<PiutangModel> getPiutangsByCustomer(CustomerModel c) {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PIUTANG_BY_CUSTOMER + "/" + c.getIdCustomer());
        return webResource.get(new GenericType<List<PiutangModel>>(){});
    }

    public List<PiutangModel> getPiutangsByCustomerBlmLns(CustomerModel c) {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PIUTANG_BY_CUSTOMER_BLMLNS + "/" + c.getIdCustomer());
        return webResource.get(new GenericType<List<PiutangModel>>(){});
    }

    public List<PiutangModel> getPiutangsBlmLunas(){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PIUTANG_BLMLNS);
        return webResource.get(new GenericType<List<PiutangModel>>(){});
    }

    public List<PiutangModel> getPiutangsLunas(){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_PIUTANG_LNS);
        return webResource.get(new GenericType<List<PiutangModel>>(){});
    }
}
